// 卡片管理
// 导入数据库
const db = require("../db/index");

// 发表认领卡片
/**
 * body
 * @param {message,img_url} req
 * @param {*} res
 */
exports.addGetGoods = (req, res) => {
  const getgoodsinfo = {
    // 内容 时间 发的用户名
    ...req.body,
    moment: new Date(),
    // 作者id
    user_id: req.user.id,
  };
  const sql = ` insert into lost_find set ?`;
  db.query(sql, getgoodsinfo, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("发布留言失败！");
    res.cc("发布留言成功", 0);
  });
};

// 获取认领卡
/**
 *
 * @param {} req
 * @param {message img_url,username,user_pic,} res
 */
exports.getgoods = (req, res) => {
  const sql = `select lost_find.* ,users.username,users.user_pic from lost_find,users where lost_find.is_delete !=1 and lost_find.user_id = users.id  order by lost_find.moment desc  `;

  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      message: "获取留言成功",
      data: results,
      status: 0,
    });
  });
};

// 获取我的求助卡
exports.getMyHelpGoods = (req, res) => {
  const sql = `select * from lost_help where user_id = ? and is_delete != 1 order by lost_help.moment`;

  db.query(sql, [req.user.id], (err, results) => {
    if (err) return err;
    if (results.length == 0) return res.cc("没有查到对应数据！");
    res.send({
      status: 0,
      message: "获取成功！",
      data: results,
    });
  });
};

// 删除个人求助卡片
/**
 *
 * @param {losthelp_id} req
 * @param {*} res
 */
exports.myHelpGoods = (req, res) => {
  const sql = `update lost_help set is_delete = ? where user_id = ? and id = ?`;
  db.query(sql, [1, req.user.id, req.query.losthelp_id], (err, results) => {
    if (err) return err;
    if (results.affectedRows !== 1) return res.cc("删除失败！稍后再试");
    res.send({
      status: 0,
      message: "删除成功！",
    });
  });
};

//
/**认领
 *  body code  0 为还原 1为更新
 * @param {lostfind_id ,code} req
 * @param {*} res
 */
exports.updateFindGoods = (req, res) => {
  const sql = `update lost_find set is_find = ?,find_id = ? where id = ?`;
  const sql2 = `select users.username from users,lost_find where users.id = lost_find.find_id and lost_find.id = ?`;
  const sql3 = `update lost_find set find_user = ? where id = ?`;
  const arr =
    req.body.code == 0
      ? [req.body.code, null, req.body.lostfind_id]
      : [req.body.code, req.user.id, req.body.lostfind_id];
  db.query(sql, arr, (err, results) => {
    if (err) return err;
    db.query(sql2, req.body.lostfind_id, (err, result) => {
      if (err) return err;
     
      const find_user = result[0] ? result[0].username : null;
     
      db.query(sql3, [find_user, req.body.lostfind_id], (err, result2) => {
        if (err) return err;
      });
      res.cc("更新成功", 0);
    });
  });
};

// 求助卡的信息
// 发表求助卡片
/**
 * body
 * @param {message,img_url} req
 * @param {*} res
 */
exports.addHelpGoods = (req, res) => {
  const getgoodsinfo = {
    // 内容 时间 发的用户名
    ...req.body,
    moment: new Date(),
    // 作者id
    user_id: req.user.id,
  };
  const sql = ` insert into lost_help set ?`;
  db.query(sql, getgoodsinfo, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("发布失败！");
    res.cc("发布成功", 0);
  });
};
// 获取求助卡
exports.getHelpGoods = (req, res) => {
  const sql = `select lost_help.* ,users.username,users.user_pic from lost_help,users where lost_help.is_delete !=1 and lost_help.user_id = users.id  order by lost_help.moment desc  `;
  db.query(sql, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      message: "获取留言成功",
      data: results,
      status: 0,
    });
  });
};

/**
 * 发布留言评论
 * @param { lost_id,message } req
 * @param {*} res
 */

exports.addHelpGoodsComments = (req, res) => {
  const sql = `insert into  lost_comment set ? `;
  const sql2 = `update lost_help set comment_num = comment_num+1 where id = ?`;
 
  const commentData = {
    ...req.body,
    user_id: req.user.id,
    moment: new Date(),
  };

  db.query(sql, commentData, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("发布评论失败！");
    db.query(sql2, req.body.lost_id, (err, result) => {
      res.cc("发布评论成功", 0);
    });
  });
};

/**
 *  获取对应卡片评论信息
 * @param {lost_id } req
 * @param {username ,message,user_pic ,moment} res
 */

exports.getHelpGoodsComments = (req, res) => {
  const sql = `SELECT lost_comment.message, lost_comment.moment, users.username, users.user_pic 
    FROM lost_comment
    INNER JOIN users ON lost_comment.user_id = users.id
    WHERE lost_comment.is_delete != 1 AND lost_comment.lost_id = ?  order by lost_comment.moment desc`;
 
  db.query(sql, [req.query.lost_id], (err, results) => {
    if (err) return res.cc(err);

    res.send({
      message: "获取评论成功",
      data: results,
      status: 0,
    });
  });
};
